//
//  NetworkManager.swift
//  BusinessIOS
//
//  Created by Rain on 2018/11/13.
//  Copyright © 2018 XingJian Li. All rights reserved.
//

import UIKit

enum NetworkMethod {
    case GET
    case POST
}

class NetworkManager: AFHTTPSessionManager {
    // 枚举 （可以是任意类型）
  
    // 创建单利 (计算型属性)
    static var sharedManager: NetworkManager {
        let tool = NetworkManager()
        tool.responseSerializer.acceptableContentTypes?.insert("application/json")
        tool.requestSerializer.timeoutInterval = 10
  
        return tool
    }

    
}
extension NetworkManager{
    // 设置默认get请求
    func request(method: NetworkMethod = .GET ,urlString: String,parameters: Any?,requestSuccess:@escaping (_ json: Any?)->(),requestFailure:@escaping (_ error: Error)->()) {
        // 定义闭包
        let success = {(task: URLSessionDataTask, json: Any)->() in
            requestSuccess(json)
        }
        let failure = {(tasks: URLSessionDataTask?, error: Error)->() in
            requestFailure(error)
        }
        if method == .GET {
            get(urlString, parameters: parameters, progress:nil, success: success, failure: failure)
        }
        if method == .POST {
            post(urlString, parameters: parameters, progress: nil, success: success, failure: failure);
        }
    }
}

